<template>
  <div>
    <van-field v-model="safety_parent" clickable readonly @click="showPicker = true">
      <template #button>
        <van-button size="small" round @click="setSafety($event)" class="setbtn">{{$t('Save')}}</van-button>
      </template>
    </van-field>
    <van-popup v-model="showPicker" round position="bottom">
      <van-picker
        show-toolbar
        :columns="columns"
        @cancel="showPicker = false"
        @confirm="onConfirm"
        :confirm-button-text="$t('OK')"
        :cancel-button-text="$t('Cancel')"
      />
    </van-popup>
  </div>
</template>

<script>
export default {
  props: ["deviceType", "safety","italyCode","reg"],
  data() {
    return {
      showPicker: false,
      safety_parent: "",
      safetyCode: this.safety,
      columns: [],
      safetJson:{}
    };
  },
  watch: {
    safety(newVal) {
      let list=this.columns;
      list.forEach((val)=>{
        if(val.text==newVal){
          this.safetJson=val;
        }
      });
      this.safetyCode = this.safetJson.code;
      this.safety_parent = this.safetJson.text;
    },
  },
  methods: {
    onConfirm(value, i) {
      this.showPicker = false;
      this.safety_parent = value.text;
      let list=this.columns;
      list.forEach((val)=>{
        if(val.text==value.text){
          this.safetJson=val;
        }
      });
      this.safetyCode = this.safetJson.value;
    },
    async setSafety(e) {
      e.stopPropagation();
      let formData = {
        'optType': "setReg",
        'Data': JSON.stringify({'num':1,'Data':[{'reg':this.reg,'val':this.safetJson.code+''}]}),
        'pwd':sessionStorage.getItem('bdPwd')
      };
      this.comm.loadingShow();
      let data = await this.SETAPI.LocalIn(formData);
      let newdata = data.substring(0,1);
      if (newdata == "Y") {
        sessionStorage.setItem("safety", this.safetJson.code);
        this.comm.toastSuccess();
        this.$emit('getFormData','success');
      }else {
        this.comm.toastFail();
      }
    },
    //获取安规列表
    //由于X1BoostPro安规不是连续的数组，所以暂时按照储能机的形式展示安规列表
    async getSafety() {
      if(this.deviceType==8||this.deviceType==9||this.deviceType==12){
        let data=this.localSafety.getSafety(this.deviceType);
        let list=[];
        if(this.italyCode==0){
          list=data['OnlyItaly'];
        }else{
          list=data['ExceptItaly'];
        }
        list.forEach(val => {
          this.columns.push({'text':val.text,'code':val.code});
          if(val.code==this.safety){
            this.safetJson={'text':val.text,'code':val.code};
          }
        });
        this.safety_parent = this.safetJson.text;
      }else{
        let list=this.localSafety.getSafety(this.deviceType);
        list.forEach((value,index)=>{
          this.columns.push({'text':value.text,'code':index});
          if(index==this.safety){
            this.safetJson={'text':value,'code':index};
          }
        });
        this.safety_parent = this.columns[this.safety].text;
      }
    },
  },
  mounted() {
    this.getSafety();
  },
};
</script>

<style scoped lang="less">
</style>